package com.pirestupppai.widgets.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.pirestupppai.R;

/**
 * 上拉加载尾
 *
 * @author: xiaoluo
 * @date: 2017-03-06 11:24
 */
public class LoadMoreFooter extends FrameLayout {
    private Context mContext;
    private ProgressBar mProgressBar;
    private TextView mEndTv;
    private Status mStatus;

    public LoadMoreFooter(Context context) {
        this(context, null);
    }

    public LoadMoreFooter(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadMoreFooter(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.widget_loadmore_footer, this, true);
        mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
        mEndTv = (TextView) findViewById(R.id.end_tv);

        setStatus(Status.GONE);
    }

    public void setStatus(Status status) {
        this.mStatus = status;
        change();
    }

    public boolean canLoadMore() {
        return mStatus == Status.GONE || mStatus == Status.ERROR;
    }

    private void change() {
        switch (mStatus) {
            case GONE:
                mProgressBar.setVisibility(GONE);
                mEndTv.setVisibility(GONE);
                break;

            case LOADING:
                mProgressBar.setVisibility(VISIBLE);
                mEndTv.setVisibility(GONE);
                break;

            case ERROR:
                mProgressBar.setVisibility(GONE);
                mEndTv.setVisibility(GONE);
                break;

            case THE_END:
                mProgressBar.setVisibility(GONE);
                mEndTv.setVisibility(VISIBLE);
                break;
        }
    }

    public enum Status {
        GONE, LOADING, ERROR, THE_END
    }
}
